﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormKhoHang : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private int idtam = 0;
        public FormKhoHang()
        {
            InitializeComponent();
        }

        private void dgvKhoHang_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvKhoHang.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
        }

        private void dgvKhoHang_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvKhoHang.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
            var f = new FormKhoHangCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }


        private void bttThemMoi_Click(object sender, EventArgs e)
        {
            var f = new FormKhoHangThem();
            f.ShowDialog();
        }

        private void bttChinhSua_Click(object sender, EventArgs e)
        {
            var f = new FormKhoHangCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void bttLamMoi_Click(object sender, EventArgs e)
        {
            GetListKhoHang();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bttTimKiem_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void GetListKhoHang()
        {
            dgvKhoHang.DataSource = db.KhoHangs.ToList();
            dgvKhoHang.Update();
            dgvKhoHang.Refresh();
            idtam = 0;
            var n = dgvKhoHang.Columns["LoaiKhoHangId"] as DataGridViewComboBoxColumn;
            n.DataSource = db.LoaiKhoHangs.ToList();
            n.DisplayMember = "TenLoaiKhoHang";
            n.ValueMember = "Id";
        }

        void GetListLoaiKhoHang()
        {
            cbbLoaiKhoHang.DataSource = db.LoaiKhoHangs.ToList();
            cbbLoaiKhoHang.DisplayMember = "TenLoaiKhoHang";
            cbbLoaiKhoHang.ValueMember = "Id";
            cbbLoaiKhoHang.DropDownColumns = "ID,TenLoaiKhoHang";
            cbbLoaiKhoHang.DropDownColumnsHeaders = "Mã loại\r\nTên loại kho hàng\r\n";
        }

        void Delete()
        {
            if (idtam != 0)
            {
                var d = db.KhoHangs.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa kho hàng: " + d.TenKhoHang + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    var t = db.KhoHangs.Find(idtam);
                    db.KhoHangs.Remove(t);
                    db.SaveChanges();
                }
            }
            else
            {
                MessageBox.Show("Ban chưa chọn dữ liệu để xóa!");
            }
            GetListKhoHang();
        }

        void TimKiem()
        {
            var idloaikho = Convert.ToInt32(cbbLoaiKhoHang.SelectedValue);
            if (txtKeyWord.Text != "" && idloaikho != 0)
            {
                dgvKhoHang.DataSource =
                    db.KhoHangs.Where(
                        t =>
                            t.LoaiKhoHangId == idloaikho &&
                            t.TenKhoHang.Contains(txtKeyWord.Text)).ToList();
            }
            else if (txtKeyWord.Text != "" && idloaikho == 0)
            {
                dgvKhoHang.DataSource =
                    db.KhoHangs.Where(
                        t =>
                            t.TenKhoHang.Contains(txtKeyWord.Text)).ToList();
            }
            else if (txtKeyWord.Text == "" && idloaikho != 0)
                {
                    dgvKhoHang.DataSource =
                        db.KhoHangs.Where(
                            t =>
                                t.LoaiKhoHangId == idloaikho).ToList();
                }
            else
            {
                GetListKhoHang();
            }
        }

        void Reset()
        {
            idtam = 0;
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }
        void KiemTra()
        {
            if (idtam != 0)
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }

        private void FormKhoHang_Load(object sender, EventArgs e)
        {
            GetListKhoHang();
            GetListLoaiKhoHang();
            Reset();
            KiemTra();
        }
    }
}
